//
//  ShopFinancialViewController.m
//  BaseFrame
//
//  Created by wingfac on 2017/8/2.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "ShopFinancialViewController.h"
#import "DataSelectView.h"
#import "ConsumptionStateView.h"
#import "FinancialInfoCell.h"
#import "FinancialSecitonHeaderView.h"

@interface ShopFinancialViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UIView *footerView;

@property (nonatomic,strong) UILabel *startLabel;
@property (nonatomic,strong) UILabel *endLabel;

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSDictionary *dataDictionary;
Assign int pstart;

@end

//网络
@implementation ShopFinancialViewController (NetWorking)

/**
 获取页面数据
 
 @param pstart 页数
 */
- (void)getData:(NSString *)pstart{
    [self showprogressHUD];
    if ([[UserModel sharedInstanced].s_id isEqualToString:@""]) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self hiddenProgressHUD];
        return;
    }
    NSDictionary *parameters = @{@"s_id":[NSString stringWithFormat:@"%@",[UserModel sharedInstanced].s_id],
                                 @"psize":@"20",
                                 @"pstart":pstart};
    [[AFAppDotNetAPIClient sharedClient] POST:@"OrderInformationMoblie/selectSid.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            if ([pstart isEqualToString:@"0"]) {
                [self.dataArray removeAllObjects];
                self.pstart = 0;
            }
            self.pstart ++;
            [self.dataArray addObjectsFromArray:responseObject[@"obj"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

/**
 修改进入店铺状态
 
 @param o_id o_id description
 */
- (void)getUpdateConsume:(NSString *)o_id{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"o_id":o_id};
    [[AFAppDotNetAPIClient sharedClient] POST:@"OrderInformationMoblie/updateConsume.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        ShowMessage(responseObject[@"msg"]);
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            [self getData:@"0"];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

/**
 通过日期查询
 */
- (void)getSelByTimeData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"s_id":[UserModel sharedInstanced].userId,
                                 @"first_time":self.startLabel.text,
                                 @"tail_time":self.endLabel.text};
    [[AFAppDotNetAPIClient sharedClient] POST:@"OrderInformationMoblie/selByTime.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            [self.dataArray removeAllObjects];
            self.pstart = 0;
            self.pstart ++;
            [self.dataArray addObjectsFromArray:responseObject[@"obj"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

//UI
@implementation ShopFinancialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"财务管理";
    [self.view addSubview:self.tableView];
    [self getData:@"0"];
    
}

- (void)timeLabelEvent:(UITapGestureRecognizer *)sender{
    DataSelectView *view = [[DataSelectView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [view show];
    
    view.selectHandle = ^(NSString *str) {
        ((UILabel *)sender.view).text = str;
        [self getSelByTimeData];
    };
}
#pragma mark - selector
- (void)applyforcash:(UIButton *)sender{
    
}
#pragma mark - UITableViewDelegate & UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40 / WIDTH_5S_SCALE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    if ([self.dataArray[section][@"o_state"] isEqualToString:@"1"]) {
//        return 55 / WIDTH_5S_SCALE;
//    }
    return 5 / WIDTH_5S_SCALE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return kFinancialInfoCellHeight;
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    FinancialSecitonHeaderView *view = [[FinancialSecitonHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 43 / WIDTH_5S_SCALE)];
    [view setTitle:@"6月销售额" content:@"222222" drawState:YES];
    
    return view;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    if (![self.dataArray[section][@"o_state"] isEqualToString:@"1"]) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5 / WIDTH_5S_SCALE)];
        view.backgroundColor = getColor(@"ebebf1");
        return view;
//    }
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 55 / WIDTH_5S_SCALE)];
//    view.backgroundColor = getColor(@"ebebf1");
//
//    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(5/WIDTH_5S_SCALE, 0, SCREEN_WIDTH-10/WIDTH_5S_SCALE, 55/WIDTH_5S_SCALE)];
//    bgView.backgroundColor = [UIColor whiteColor];
//    [view addSubview:bgView];
//    
//    UIButton *drawButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    drawButton.frame = CGRectMake((SCREEN_WIDTH - 200/WIDTH_5S_SCALE)/2, 10/WIDTH_5S_SCALE, 200/WIDTH_5S_SCALE, 30/WIDTH_5S_SCALE);
//    drawButton.layer.cornerRadius = 15/WIDTH_5S_SCALE;
//    drawButton.backgroundColor = getColor(mainColor);
//    [drawButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    drawButton.titleLabel.font = DEF_FontSize_13;
//    [drawButton setTitle:@"申请提现" forState:UIControlStateNormal];
//    drawButton.tag = 2000+section;
//    [drawButton addTarget:self action:@selector(applyforcash:) forControlEvents:UIControlEventTouchUpInside];
//    [bgView addSubview:drawButton];
//    
//    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 50/WIDTH_5S_SCALE, SCREEN_WIDTH, 5/WIDTH_5S_SCALE)];
//    line.backgroundColor = getColor(@"ebebf1");
//    [view addSubview:line];
    return view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    return self.dataArray.count;
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FinancialInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FinancialInfoCell class])];
    if(!cell){
        cell = [[FinancialInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([FinancialInfoCell class])];
        [cell setTitle:@"11" content:@"222" textColor:[UIColor blackColor]];;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark - set & get

- (UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 5;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footerView;
        _tableView.tableHeaderView = self.headerView;
        _tableView.rowHeight = 180;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;

        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getData:@"0"];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self getData:[NSString stringWithFormat:@"%d",self.pstart]];
        }];
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 36)];
        
        _startLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH/2 - 100)/2, 8, 100, 20)];
        _startLabel.backgroundColor = [UIColor whiteColor];
        _startLabel.layer.masksToBounds = YES;
        _startLabel.layer.cornerRadius = 10;
        _startLabel.text = @"--请选择日期--";
        _startLabel.font = DEF_FontSize_12;
        _startLabel.textAlignment = NSTextAlignmentCenter;
        _startLabel.userInteractionEnabled = YES;
        [_startLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(timeLabelEvent:)]];
        
        _endLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2.+(SCREEN_WIDTH/2 - 100)/2, 8, 100, 20)];
        _endLabel.backgroundColor = [UIColor whiteColor];
        _endLabel.layer.masksToBounds = YES;
        _endLabel.layer.cornerRadius = 10;
        _endLabel.text = @"--请选择日期--";
        _endLabel.font = DEF_FontSize_12;
        _endLabel.textAlignment = NSTextAlignmentCenter;
        _endLabel.userInteractionEnabled = YES;
        [_endLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(timeLabelEvent:)]];
        
        [_headerView addSubview:_startLabel];
        [_headerView addSubview:_endLabel];
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (NSDictionary *)dataDictionary{
    if (!_dataDictionary) {
        _dataDictionary = @{};
    }
    return _dataDictionary;
}

@end
